//
//  UIImage+Extension.swift
//  Weibo
//
//  Created by 黄亦寒 on 2016/9/30.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
extension UIImage {
    //截取当前window图片的方法
    static func screenShot() -> UIImage {
        let window = UIApplication.shared.keyWindow!
        //开启图片上下文
        UIGraphicsBeginImageContext(window.size)
        
        window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
        //从图片上下文中获取图片
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        //关闭图片上下文
        UIGraphicsEndImageContext()
        
        return image!
    }
    
    //根据指定的宽度等比压缩图片的方法，防止内存暴涨
    func scaleImage(scaleWidth:CGFloat) -> UIImage {
        //压缩后的高度
        let scaleHeight = scaleWidth / self.size.width * self.size.height
        //压缩后的大小
        let size = CGSize(width:scaleWidth,height:scaleHeight)
        //开启图片上下文
        UIGraphicsBeginImageContext(size)
        //把图片绘制到指定区域内
        self.draw(in: CGRect(origin: CGPoint.zero, size: size))
        //获取压缩后的图片
        let scaleImage = UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        
        return scaleImage!
        
    }
}
